package dao

import (
	"time"

	"github.com/go-xorm/xorm"

	"im/common"
	"im/dao/es"
	"im/errcode"
	"im/model"
	"im/requests"
	"im/service/orm"
)

func NewGroupsAdmins() groupsAdmins {
	return groupsAdmins{db: orm.GetDB()}
}

type groupsAdmins struct {
	db *xorm.Engine
}

func (p groupsAdmins) GetByIds(ids ...int64) ([]model.Groups, error) {
	var ms = []model.Groups{}
	err := p.db.In("id", common.ToInterfaceSlice(ids)...).Find(&ms)
	if err != nil {
		return ms, errcode.NewError(errcode.DaoFailed, err)
	}
	return ms, nil
}
func (p groupsAdmins) IndexDoc(id int64, m model.Groups, wait bool) error {
	esM := EsGroupModel{
		Groups: m,
		TimeModelOfEs: model.TimeModelOfEs{
			time.Time(m.TimeModel.Created),
			time.Time(m.TimeModel.Updated),
		},
	}
	err := es.New().IndexDocWait(IndexNameOfGroups, id, esM, wait)
	return err
}

func (p groupsAdmins) List(pagination requests.Pagination) ([]model.Groups, int64, error) {
	var groups []model.Groups
	count, err := p.db.Limit(pagination.Limit, pagination.GetOffset()).FindAndCount(&groups)
	return groups, count, err
}
